// components/swiper2/swiper2.js
const {common: common} = require("../../common/common");

Component({
    /**
   * 组件的属性列表
   */
    properties: {
        duration: {
            type: Number,
            value: 500
        },
        easingFunction: {
            type: String,
            value: "default"
        },
        loop: {
            type: Boolean,
            value: true
        },
        comeon: {
            type: Number,
            value: 1
        },
        currentIndex: {
            type: Number,
            value: 0
        },
        videoList: {
            type: Array,
            value: [],
            observer: function() {
                this.setData({
                    videoContext: wx.createVideoContext(`video_${this.data.currentIndex}`, this)
                });
                // this.play();
            }
            // observer: function observer() {
            //     var newVal = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
            //     this._videoListChanged(newVal);
            // }
                },
        videoAlert: {
            type: Boolean,
            value: true,
            observer: function(newVal) {
                if (newVal) {
                    this.pause();
                } else {
                    this.play();
                }
            }
        }
    },
    /**
   * 组件的初始数据
   */
    data: {
        nextQueue: [],
        prevQueue: [],
        curQueue: [],
        circular: false,
        _last: 1,
        _change: -1,
        _invalidUp: 0,
        _invalidDown: 0,
        _videoContexts: [],
        subX: "",
        subXs: "",
        arr: [],
        videoContext: [],
        preIndex: "",
        nowIndex: ""
    },
    lifetimes: {
        attached: function attached() {
            this.data._videoContexts = [ wx.createVideoContext("video_0", this), wx.createVideoContext("video_1", this), wx.createVideoContext("video_2", this) ];
        }
    },
    /**
   * 组件的方法列表
   */
    methods: {
        play() {
            this.data.videoContext.play();
        },
        pause() {
            this.data.videoContext.pause();
        },
        changeS(e) {
            this.setData({
                currentIndex: e.detail.current
            });
        },
        //触碰开始
        touchstart(e) {
            this.data.subX = e.changedTouches[0].clientY;
            let arr = [];
            arr.push;
            this.setData({
                preIndex: this.data.currentIndex,
                arr: [ this.data.videoList[this.data.currentIndex] ],
                videoContext: wx.createVideoContext(`video_${this.data.currentIndex}`, this)
            });
        },
        //触碰结束
        touchend(e) {
            this.data.subXs = e.changedTouches[0].clientY;
            var arr = this.data.arr;
            arr.push(this.data.videoList[this.data.currentIndex]);
            this.setData({
                arr: arr,
                nowIndex: this.data.currentIndex
            });
            // if (this.data.arr[0].id === this.data.videoList[0].id &&this.data.arr[0].id === this.data.arr[1].id &&this.data.subX !== this.data.subXs&&this.data.preIndex==0
            // ) {
            //     wx.showToast({
            //         title: '没有更多内容了'
            //     });
            // } else if (this.data.arr[0].id === this.data.videoList[this.data.videoList.length - 1].id &&this.data.arr[0].id === this.data.arr[1].id &&this.data.subX !== this.data.subXs&&this.data.preIndex==this.data.videoList.length -1
            // ) {
            //     wx.showToast({
            //         title: '没有更多内容了'
            //     });
            // }
                        if (this.data.arr[0].id != this.data.arr[1].id) {
                // this.setData({
                //   videoContext:wx.createVideoContext(String(this.data.arr[0].id), this) 
                // })
                this.data.videoContext.pause();
            }
        },
        getVideoPlay(id, index) {
            let that = this;
            common.request({
                endpoint: "video/video_play",
                data: {
                    source: 1,
                    id: id
                },
                method: "post",
                success: res => {
                    let videoList = that.data.videoList;
                    videoList[index].video = res.data;
                    that.setData({
                        videoList: videoList
                    });
                }
            });
        },
        animationfinish: function animationfinish(e) {
            var current = e.detail.current;
            var _last = this.data._last;
            var diff = current - _last;
            var direction = diff == 1 || diff == -2 ? "up" : "down";
            if (diff === 0) return;
            this.setData({
                _last: current
            });
            let videoList = this.data.videoList;
            var changeC = 0;
            if (direction == "up") {
                if (current == 0) {
                    changeC = 1;
                } else {
                    changeC = current == 1 ? 2 : 0;
                }
            } else {
                if (current == 0) {
                    changeC = 2;
                } else {
                    changeC = current == 1 ? 0 : 1;
                }
            }
            this.triggerEvent("change", {
                activeId: videoList[current].id,
                changeC: changeC,
                direction: direction
            });
            if (videoList[current].video_type == 1 || videoList[current].video_type == 2) {
                this.getVideoPlay(videoList[current].id, current);
            }
        },
        playCurrent: function playCurrent(current) {
            this.data._videoContexts.forEach(function(ctx, index) {
                index !== current ? ctx.pause() : ctx.play();
            });
        },
        onPlay: function onPlay(e) {
            this.trigger(e, "play");
        },
        onPause: function onPause(e) {
            this.trigger(e, "pause");
        },
        onEnded: function onEnded(e) {
            this.trigger(e, "ended");
        },
        onError: function onError(e) {
            this.trigger(e, "error");
        },
        onTimeUpdate: function onTimeUpdate(e) {
            this.trigger(e, "timeupdate");
        },
        onWaiting: function onWaiting(e) {
            this.trigger(e, "wait");
        },
        onProgress: function onProgress(e) {
            this.trigger(e, "progress");
        },
        onLoadedMetaData: function onLoadedMetaData(e) {
            this.trigger(e, "loadedmetadata");
        },
        trigger: function trigger(e, type) {
            var ext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
            var detail = e.detail;
            var activeId = e.target.dataset.id;
            this.triggerEvent(type, Object.assign(Object.assign(Object.assign({}, detail), {
                activeId: activeId
            }), ext));
        }
    }
});